<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" @submit.prevent>
      <!-- 新增所属街道下拉框查询条件 -->
      <el-form-item label="所属街道" prop="streetId">
        <el-select
          v-model="queryParams.streetId"
          placeholder="请选择街道"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="street in streetList"
            :key="street.streetId"
            :label="street.streetAddress"
            :value="street.streetId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="内容标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入内容标题"
          clearable
          @keyup.enter="handleQuery"
          v-hasPermi="['htzj:lswh:list']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['htzj:lswh:list']">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['htzj:lswh:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <!-- 旅游景区列表 -->
    <el-table v-loading="loading" :data="attractionList">
      <el-table-column label="内容标题" align="center" prop="title" />
      <el-table-column label="所属街道" align="center" prop="streetName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['htzj:lswh:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['htzj:lswh:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/修改对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="attractionFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属街道" prop="streetId">
          <el-select v-model="form.streetId" placeholder="请选择街道" clearable>
            <el-option
              v-for="street in streetList"
              :key="street.streetId"
              :label="street.officeName"
              :value="street.streetId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="内容标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入内容标题" />
        </el-form-item>
        <el-form-item label="富文本内容" prop="richContent">
          <!-- 假设这里是若依自带的富文本编辑器 -->
          <editor v-model="form.richContent" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listContents, delContents, addContents, updateContents  } from "@/api/htzj/contents";
import { listStreet } from "@/api/htzj/streets";
import { ElMessage, ElMessageBox } from 'element-plus'

// 加载状态
const loading = ref(false)
// 总记录数
const total = ref(0)
// 旅游景区列表
const attractionList = ref([])
// 是否显示搜索表单
const showSearch = ref(true)

// 查询参数，新增 streetId
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  contentType: 'attraction',
  title: undefined,
  streetId: undefined // 新增查询参数
})

// 处理查询
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置查询，新增重置 streetId
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    contentType: 'attraction',
    title: undefined,
    streetId: undefined // 新增重置参数
  };
  getList();
}

// 获取旅游景区列表
const getList = async () => {
  loading.value = true;
  try {
    const res = await listContents(queryParams.value);
    attractionList.value = res.rows;
    total.value = res.total;
  } catch (error) {
    console.error("获取旅游景区列表失败", error);
  } finally {
    loading.value = false;
  }
}

// 删除旅游景区信息
const handleDelete = (row) => {
  const contentId = row.contentId;
  ElMessageBox.confirm('是否确认删除该旅游景区信息？', "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(function() {
    return delContents(contentId);
  }).then(() => {
    getList();
    ElMessage.success("删除成功");
  }).catch(() => {});
}

// 对话框是否打开
const open = ref(false);
// 对话框标题
const title = ref("");
// 表单引用
const attractionFormRef = ref();
// 表单数据
const form = ref({
  contentId: undefined,
  streetId: undefined,
  contentType: 'attraction',
  title: undefined,
  richContent: undefined
});

// 表单验证规则
const rules = ref({
  streetId: [
    { required: true, message: "请选择所属街道", trigger: "change" }
  ],
  title: [
    { required: true, message: "请输入内容标题", trigger: "blur" }
  ],
  richContent: [
    { required: true, message: "请输入富文本内容", trigger: "blur" }
  ]
});

// 街道列表
const streetList = ref([]);

// 获取街道列表
const getStreetList = async () => {
  try {
    const res = await listStreet();
    streetList.value = res;
  } catch (error) {
    console.error("获取街道列表失败", error);
  }
}

// 新增旅游景区信息
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加旅游景区信息";
};

// 修改旅游景区信息
const handleUpdate = (row) => {
  reset();
  const contentId = row.contentId;
  form.value = { ...row };
  open.value = true;
  title.value = "修改旅游景区信息";
};

// 重置表单
const reset = () => {
  form.value = {
    contentId: undefined,
    streetId: undefined,
    contentType: 'attraction',
    title: undefined,
    richContent: undefined
  };
  if (attractionFormRef.value) {
    attractionFormRef.value.resetFields();
  }
};

// 取消操作
const cancel = () => {
  open.value = false;
  reset();
};

// 提交表单
const submitForm = () => {
  attractionFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        if (form.value.contentId) {
          await updateContents(form.value);
          ElMessage.success("修改成功");
        } else {
          await addContents(form.value);
          ElMessage.success("新增成功");
        }
        open.value = false;
        getList();
      } catch (error) {
        console.error(error);
        ElMessage.error("操作失败");
      }
    }
  getStreetList(); // 确保页面加载时获取街道列表
  });
};

onMounted(() => {
  getList();
  getStreetList();
})
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  padding: 20px;
  text-align: right;
}
</style>
